package com.example.mybatisplus.web.controller;

import com.example.mybatisplus.mapper.AttributeMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AttributeService;
import com.example.mybatisplus.model.domain.Attribute;

import java.util.HashMap;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-03-11
 * @version v1.0
 */
@Controller
@RequestMapping("/api/attribute")
public class AttributeController {

    private final Logger logger = LoggerFactory.getLogger(AttributeController.class);

    @Autowired
    private AttributeMapper attributeMapper;

    @Autowired
    private AttributeService attributeService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Attribute  attribute =  attributeService.getById(id);
        return JsonResponse.success(attribute);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        attributeService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateAttribute(Attribute  attribute) throws Exception {
        attributeService.updateById(attribute);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Attribute
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Attribute  attribute) throws Exception {
        attributeService.save(attribute);
        return JsonResponse.success(null);
    }

    @GetMapping("/fivePercentage")
    public ResponseEntity<Map<String, Double>> getThreePercentage() {
        Double wifi = attributeMapper.getWifi();
        Double credit = attributeMapper.getCredit();
        Double kid = attributeMapper.getKid();
        Double wheel = attributeMapper.getWheel();
        Double group = attributeMapper.getGroup();
        Map<String, Double> response = new HashMap<>();
        response.put("wifiPercentage", wifi);
        response.put("creditPercentage", credit);
        response.put("kidPercentage", kid);
        response.put("wheelPercentage", wheel);
        response.put("groupPercentage", group);
        return ResponseEntity.ok(response);
    }
}

